#ifndef _MODULE_MWCAPTCHA
#define _MODULE_MWCAPTCHA
#include "page.module.js"
#include "prefs.module.js"
#include "usolib.lib.js"

Mwcaptcha = {
	trans_id: '',
	image: '',
	solution: '',
	username: '',
	password: '',
	stop: false,
	solving: false,
	getting: false,
	add: function(image) {
		if (!Settings.mwcaptcha) return;
		if (Settings.mwcaptcha_safe) GM_xmlhttpRequest({
			method: 'GET',
			url: image,
			overrideMimeType: 'text/plain; charset=x-user-defined',
			onload: function(xhr) {
				var data = '';
				for (var i = 0, il = xhr.responseText.length; i < il; i++) data += String.fromCharCode((xhr.responseText[i].charCodeAt(0) & 0xff));
				data = btoa(data);
				$('#captcha_image').attr('src', 'data:image/gif;base64,' + data);
				GM_xmlhttpRequest({
					method: 'POST',
					headers: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					url: 'https://www.mwturbo.com/mwcaptcha/api.php',
					data: 'client=mwturbo&act=add&username=' + escape(Mwcaptcha.username) + '&password=' + escape(Mwcaptcha.password) + '&image=' + encodeURIComponent('data:image/gif;base64,' + data),
					onload: function(xhr) {
						Mwcaptcha.handle_response( xhr.responseText, function( resp ) {
							if ( Mwcaptcha.stop === false ) {
								Mwcaptcha.trans_id = resp;
								if (Mwcaptcha.getting == false) {
									Mwcaptcha.getting = true;
									Mwcaptcha.get();
								}
							}
						} );
					}
				});
				return;
			}
		});
		else GM_xmlhttpRequest({
			method: 'POST',
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			url: 'https://www.mwturbo.com/mwcaptcha/api.php',
			data: 'client=mwturbo&act=add&username=' + escape(Mwcaptcha.username) + '&password=' + escape(Mwcaptcha.password) + '&image=' + encodeURIComponent(image),
			onload: function(xhr) {
				Mwcaptcha.handle_response( xhr.responseText, function( resp ) {
					if (Mwcaptcha.stop == false) {
						Mwcaptcha.trans_id = resp;
						if (Mwcaptcha.getting == false) {
							Mwcaptcha.getting = true;
							Mwcaptcha.get();
						}
					}
				} );
			}
		});
	},
	get: function() {
		if (!Settings.mwcaptcha) return;
		GM_xmlhttpRequest({
			method: 'POST',
			url: 'https://www.mwturbo.com/mwcaptcha/api.php',
			data: 'client=mwturbo&act=get&trans_id=' + escape(Mwcaptcha.trans_id),
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			onload: function(xhr) {
				Mwcaptcha.handle_response( xhr.responseText, function( resp ) {
					if ( resp === "TRANS_ID" )
						Page.captcha_submit('0');
					else if ( resp === "CONTINUE" && Mwcaptcha.stop === false ) {
						if (Mwcaptcha.getting == true) {
							Mwcaptcha.get();
							return;
						}
					}
					else if ( Mwcaptcha.stop == false )
						Page.captcha_submit(resp);
					Mwcaptcha.getting = false;
				} );
			}
		});
	},
	result: function(success) {
		if (success == false)
			success = "FAIL";
		else
			success = "SUCCESS";
		GM_xmlhttpRequest({
			method: 'POST',
			url: 'https://www.mwturbo.com/mwcaptcha/api.php',
			data: 'client=mwturbo&act=result&trans_id=' + escape(Mwcaptcha.trans_id) + '&result=' + escape(success),
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			onload: function(xhr) {
				Mwcaptcha.handle_response( xhr.responseText, function( resp ) {} );
			}
		});
	},
	solve: function() {
		if ( !Settings.mwcaptcha )
			return;
		GM_xmlhttpRequest({
			method: 'POST',
			url: 'https://www.mwturbo.com/mwcaptcha/api.php',
			data: 'client=mwturbo&act=solve&username=' + escape(Mwcaptcha.username) + '&password=' + escape(Mwcaptcha.password),
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			onload: function(xhr) {
				Mwcaptcha.handle_response( xhr.responseText, function( resp ) {
					if (resp == "CONTINUE" && Mwcaptcha.stop === false) {
						if (Settings.mwc_earn === true && Mwcaptcha.solving === true) {
							Mwcaptcha.solve();
							return;
						}
					} else if (Mwcaptcha.stop === false) {
						Mwcaptcha.image = resp;
						Mwcaptcha.solving = false;
						Page.show_mwcaptcha();
					}
					Mwcaptcha.solving = false;
				} );
			}
		});
	},
	answer: function() {
		GM_xmlhttpRequest({
			method: 'POST',
			url: 'https://www.mwturbo.com/mwcaptcha/api.php',
			data: 'client=mwturbo&act=answer&username=' + escape(Mwcaptcha.username) + '&password=' + escape(Mwcaptcha.password) + '&answer=' + escape(Mwcaptcha.solution),
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			onload: function(xhr) {
				Mwcaptcha.handle_response( xhr.responseText, function( resp ) {
					if (resp == "EXPIRED" && Mwcaptcha.stop === false) {
						if (Settings.mwc_earn == true && Mwcaptcha.solving == false) {
							Mwcaptcha.solving = true;
							Mwcaptcha.solve();
						}
					} else if (resp == "SUCCESS" && Mwcaptcha.stop == false) {
						if (Settings.mwc_earn == true && Mwcaptcha.solving == false) {
							Mwcaptcha.solving = true;
							Mwcaptcha.solve();
						}
					}
				} );
			}
		});
	},
	points: function() {
		GM_xmlhttpRequest({
			method: 'POST',
			url: 'https://www.mwturbo.com/mwcaptcha/api.php',
			data: 'client=mwturbo&act=points&username=' + escape(Mwcaptcha.username),
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			onload: function(xhr) {
				Mwcaptcha.handle_response( xhr.responseText, function( resp ) {
					alert('You have ' + resp + ' MWcaptcha Captcha points.');
				} );
			}
		});
	},
	handle_response: function( response, callback ) {
		switch ( response ) {
			case "USERNAME":
				alert('MWcaptcha Username is missing!');
				break;
			case "PASSWORD":
				alert('MWcaptcha Password is missing or incorrect!');
				break;
			case "SUBMIT":
				break;
			case "ANSWER":
				alert('MWcaptcha required field is missing!');
				break;
			case "POINTS":
				alert('You don\t have enough MWcaptcha points!');
				break;
			case "EXPIRED":
				alert('You took too long! You only have 10 seconds to solve someone elses Captcha!');
			case "SUSPEND":
				alert('Your MWcaptcha account has been suspended!');
				break;
			case "OFFLINE":
				alert('MWcaptcha service is current offline until further notice.');
				break;
			default:
				callback( response );
				break;
		}
	}
};
#endif
